package com.bwie.auth.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.bwie.auth.exception.LoginException;
import com.bwie.auth.remote.UserRemote;
import com.bwie.auth.service.AuthUserService;
import com.bwie.common.constants.JwtConstants;
import com.bwie.common.constants.LoginConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.UserRequest;
import com.bwie.common.domain.response.UserResponse;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Struct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @className: AuthUserServiceImpl
 * @Description: TODO
 * @version: v1.８.0
 * @author: 张雨乐
 * @date: 2025/9/18 9:28
 */
@Service
public class AuthUserServiceImpl implements AuthUserService {
    @Autowired
    UserRemote userRemote;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public Result login(UserRequest userRequest) {
        //	用户登录5分钟内最多尝试登录3次（3分）
        if (redisTemplate.hasKey(userRequest.getName())){
            throw new LoginException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        String name = userRequest.getName();
        String pwd = userRequest.getPwd();
        if (StrUtil.isBlank(name)){
            throw new LoginException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        if (StrUtil.isBlank(pwd)){
            throw new LoginException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        Result<User> result=userRemote.login(userRequest);
        User user = result.getData();
        if (user==null){
            Integer o = (Integer) redisTemplate.opsForValue().get(JwtConstants.DETAILS_USER_ID + name);
            if (o>=3){
                redisTemplate.opsForValue().set(name,0,5,TimeUnit.MINUTES);
                redisTemplate.delete(JwtConstants.DETAILS_USER_ID + name);
                //	请实现密码校验重试机制，最大重试次数为3次（2分）
                throw new LoginException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
            }
            if (o>0){
                Integer i = o++;
                redisTemplate.opsForValue().set(JwtConstants.DETAILS_USER_ID + name,i,1, TimeUnit.MINUTES);
            }else {
                redisTemplate.opsForValue().set(JwtConstants.DETAILS_USER_ID + name,1,1, TimeUnit.MINUTES);
            }
            throw new LoginException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        Map<String, Object> map = new HashMap<>();
        String userKey = IdUtil.fastSimpleUUID();
        map.put(JwtConstants.USER_KEY,userKey);
        String token = JwtUtils.createToken(map);
        UserResponse userResponse = new UserResponse();
        redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY + userKey,user,30,TimeUnit.MINUTES);
        userResponse.setToken(token);
        userResponse.setTime("30TIME");
        return Result.success(userResponse);
    }
}
